package com.chengzzz.mysite.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengzzz.mysite.dao.AnimationDao;
import com.chengzzz.mysite.entity.Animation;
import com.chengzzz.mysite.entity.User;
import com.chengzzz.mysite.enums.ResultEnum;
import com.chengzzz.mysite.result.Result;
import com.chengzzz.mysite.service.AnimationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * (Animation)表服务实现类
 *
 * @author 等什么柠檬君
 * @since 2020-08-24 14:26:49
 */
@Service("animationService")
public class AnimationServiceImpl extends ServiceImpl<AnimationDao, Animation> implements AnimationService {

    @Autowired
    private AnimationDao animationDao;

    @Override
    public List<Animation> selectByUid(int id) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("uid", id);
        return animationDao.selectList(queryWrapper);
    }

    @Override
    public List<Map<String, String>> selectAS() {
        return animationDao.selectMulti();
    }

    @Override
    public IPage<Animation> selectByPage(Integer page, Integer size) {
        Page<Animation> amtPage = new Page<>(page, size);

        return    animationDao.selectPage(amtPage,null);
    }

    @Override
    public Result AnimationDelete(Long uuid) {
        int num = animationDao.deleteById(uuid);
        if (num != 0) {
            return new Result(ResultEnum.DELETESUCESS.getCode(), "逻辑删除成功", "数量：" + num, true);
        }
        return new Result(ResultEnum.DELETEERROR.getCode(), "逻辑删除失败", "数量：" + num, false);
    }

    @Override
    public Result AnimationUpdate(Animation animation) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("aid",animation.getAid());
        int num = animationDao.update(animation,queryWrapper);
        System.out.println(num);
        if (num != 0) {
            return new Result(ResultEnum.SUCCESS.getCode(), "更新成功", "数量：" + num, true);
        }
        return new Result(ResultEnum.ERROR.getCode(), "更新失败", "数量：" + num, false);
    }

    @Override
    public Result insertAnimation(Animation animation) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name",animation.getName());
        int anum = animationDao.selectCount(queryWrapper);
        if (anum!=0){
            return new Result(ResultEnum.ERROR.getCode(), "插入失败，此番剧已存在", "数量："+anum , false);
        }
        int num = animationDao.insert(animation);
        if (num!=1){
            return new Result(ResultEnum.ERROR.getCode(), "插入失败，此番剧已存在", "数量：" +num, false);
        }
        return new Result(ResultEnum.SUCCESS.getCode(), "插入成功", "数量：" + num, true);




    }
}